<?
require_once('./lib/authentication.lib.php');
require_once('./lib/sort.lib.php');
require_once('./lib/navigation.lib.php');
require_once('./lib/game.lib.php');

if (isset($_GET["champ"]) and isset($_GET["referee"]))
{
	$champ_id = mysql_escape_string($_GET["champ"]);

	$query = "SELECT * FROM Championship WHERE id = '$champ_id';";
	$result_champ = mysql_query($query);
	if (mysql_numrows($result_champ) != 1)
	{
		die("Invalid champ");
	}

	$champ_name = mysql_result($result_champ, 0, "name");
	$champ_begin = mysql_result($result_champ, 0, "begin");
	$champ_end = mysql_result($result_champ, 0, "end");

	$champ_begin = substr($champ_begin, 0, 4);
	$champ_end = substr($champ_end, 0, 4);
	?>
	<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
	        "http://www.w3.org/TR/REC-html40/loose.dtd">
	<HTML>
	<?
	echo "<HEAD><TITLE>$champ_name $champ_begin" . (($champ_begin !== $champ_end) ? "/$champ_end" : "") . "</TITLE>\n";
	?>
	<SCRIPT TYPE="text/javascript" SRC="js/game.js"></SCRIPT>
	<SCRIPT TYPE="text/javascript" SRC="js/game_squad.js"></SCRIPT>
	<SCRIPT TYPE="text/javascript" SRC="js/game_info.js"></SCRIPT>
	<SCRIPT TYPE="text/javascript" SRC="js/sorttable.js"></SCRIPT>
	<SCRIPT TYPE="text/javascript" SRC="js/tabs.js"></SCRIPT>
	<link rel="stylesheet" href="css/style.css" type="text/css">
	<link rel="stylesheet" href="css/squad.css" type="text/css">
	<STYLE type="text/css">
	<!--
	div.tab_page
	{
	height: 60%;
	width: 800px;
	overflow: auto;
	border: 1px solid #666;
	background-color: #ccc;
	}
	.game_score
	{
	width: 100%;
	padding: 0px;
	border-spacing: 0px;
	}
	.game_info
	{
	width: 100%;
	}
	#Results
	{
	position: absolute;
	height: 40%;
	left: 200px;
	width: 800px;
	overflow: auto;
	border: 1px solid #666;
	background-color: #ccc;
	}
	#totals
	{
	position: absolute;
	height: 40%;
	width: 200px;
	overflow: auto;
	border: 1px solid #666;
	background-color: #ccc;
	}
	-->
	</STYLE>
	<BODY>

	<CENTER>
	<? echo foot_header($champ_id, "referee"); ?>
	</CENTER>

<?
	$referee_id = mysql_escape_string($_GET["referee"]);

	$query = "SELECT name, location FROM Referee WHERE id = '$referee_id';";	
	$result_referee = mysql_query($query);
	if (mysql_numrows($result_referee) != 1)
	{
		die("Invalid referee");
	}

	$referee_name = mysql_result($result_referee, 0, "name");
	$referee_location = mysql_result($result_referee, 0, "location");

	echo "<H1>$referee_name".($referee_location ? " - $referee_location" : "") . "</H1>\n";
	
	$game_ids = "''";

	$query = "select Game.id, Game.home, Game.away from Phase, Game where Game.phase = Phase.id and Game.referee = '$referee_id' and Phase.championship = '$champ_id';";
	$result_game = mysql_query($query);
	$num_game = mysql_numrows($result_game);
	$total_goals = 0;
	$home_goals = 0;
	$away_goals = 0;
	$first_goals = 0;
	$second_goals = 0;
	$pen_goals = 0;
	$own_goals = 0;
	$max_goals = 0;
	$hat_tricks = 0;
	for ($i = 0; $i < $num_game; $i++)
	{
		$game_id = mysql_result($result_game, $i, "Game.id");
		$game_home = mysql_result($result_game, $i, "Game.home");
		$game_away = mysql_result($result_game, $i, "Game.away");

		$game_ids .= ", '$game_id'";
	}

	echo "<div id='totals'>\n";
	echo "<table>\n";
	echo "<tr><td>Total:</td><td>$num_game</td></tr>\n";
	echo "</table>\n";
	echo "</div>\n";
	
	$query = "SELECT Game.id game_id, Game.home, Game.away, Game.phase, Game.home_score, Game.away_score, Game.home_pen, Game.away_pen, Game.attendance, Game.date, Game.round, Game.played, Referee.name referee_name, Referee.location referee_location, Stadium.name stadium_name, Home.name home_name, Home.logo home_logo, Away.name away_name, Away.logo away_logo FROM Game LEFT JOIN Team as Home ON Game.home = Home.id LEFT JOIN Team as Away ON Game.away = Away.id LEFT JOIN Referee ON Game.referee = Referee.id LEFT JOIN Stadium ON Game.stadium = Stadium.id WHERE Game.id IN ($game_ids) AND Game.played = 'played' ORDER BY Game.date ASC;";
	$result_game = mysql_query($query);

	echo "<DIV ID='Results'>\n";

	echo "<table class='sortable' id='games_table'>\n";
	echo "<tr class='table_head'><th>Date</th><th>Game</th><th></th><th></th></tr>\n";
	while ($row = mysql_fetch_assoc($result_game))
	{
		echo "<tr class='table_line'>\n";

		$date_str = date("d/m/Y", strtotime($row["date"]));

		echo "<td class='game_inline_date' style='vertical-align: top'>$date_str</td>\n";

		echo "<td width='100%' style='vertical-align: top'>\n";
		echo write_game_score($row);

		echo "<DIV class='game_info'>\n";
#		echo write_game_info($row);
		echo "</div>\n";

		echo "</td>\n";

		echo "<td class='show_info'><span onClick='writeInfo(".$row["game_id"].", this.parentNode.parentNode, this);'>Info</span></td>\n";
		echo "<td class='edit'><a href='javascript:edit_game($champ_id, ".$row["game_id"].");'>Edit</a></td>\n";

		echo "</tr>\n";
	}
	echo "</table>\n";
	echo "</div>\n";
	?>

<DIV>
<CENTER>
<? echo foot_footer($championship, "referee"); ?>
</CENTER>
</DIV>


</BODY>
</HTML>
	<?
}
?>
